package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.domain.OperateRoleDO;
import com.smart.model.request.OperateRoleRequest;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface OperateRoleMapper extends BaseMapper<OperateRoleDO> {
    OperateRoleDO selectDOById(Long id);
    List<OperateRoleDO> selectDOList(OperateRoleDO operateRole);
    int insertDO(OperateRoleDO operateRole);
    int batchInsertDO(List<OperateRoleDO> operateRoleDOList);
    int updateDO(OperateRoleDO operateRole);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from operate_role",
            "   <where>",
            "       deleted=0 and id!=1",
            "       <if test=\"param.roleName!=null and param.roleName!=''\">and role_name like concat('%',#{param.roleName},'%')</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") OperateRoleRequest param);
    @Select({"<script>",
            "   select * from operate_role",
            "   <where>",
            "       deleted=0 and id!=1",
            "       <if test=\"param.roleName!=null and param.roleName!=''\">and role_name like concat('%',#{param.roleName},'%')</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<OperateRoleDO> pageQuery(@Param("param") OperateRoleRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select * from operate_role where role_name=#{roleName} limit 1")
    OperateRoleDO findByName(String roleName);

    @Delete("delete from operate_role where role_id = #{roleId}")
    Integer deleteByRoleId(Long id);

    @Select("select count(1) from admin_role where role_id = #{roleId}")
    long countByRoleId(long roleId);

    @Select("select r.* from operate_role r inner join operate_user_role ur on r.id=ur.role_id where ur.user_id=#{operateUserId}")
    List<OperateRoleDO> findByUserId(Long operateUserId);
}
